﻿Imports NewHeaderSystems.Archive.Engine
Imports System.IO
Imports NewHeaderSystems.Archive.Data

Public Class StartArchiving
    Inherits PageTemplate

    Private _redirectToDownload As Boolean = False

    Private Enum FormStatus
        Idle
        Processing
    End Enum

    Private Sub StartArchiving_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Title = "Start Archiving"
        Description = "Select from the available source confirm files, then click 'Start Archive'."
        PageIcon = My.Resources.StartArchiving
    End Sub

    Private Sub btnStartArchiving_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartArchiving.Click
        StartProcessing()
    End Sub

    Private Sub btnFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinish.Click
        GoHome()
    End Sub

    Private Sub backgroundArchiver_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles backgroundArchiver.DoWork

        Dim archiver As New Processor(backgroundArchiver)
        Dim fileList() As String = CType(e.Argument, String())
        e.Result = archiver.Start(fileList, App.Instance.UserID)
    End Sub

    Private Sub SetFormStatus(ByVal status As FormStatus)
        Dim showBusy As Boolean
        showBusy = (status = FormStatus.Processing)
        If showBusy Then            
            pbProgress.Value = 0
        End If
        lblStatus.Text = String.Empty
        picArchiving.Visible = showBusy
        pbProgress.Visible = showBusy
        pbProgress.Enabled = showBusy
        btnStartArchiving.Enabled = Not showBusy
        btnFinish.Enabled = Not showBusy
        chkArchiveFileList.Enabled = Not showBusy
        Me.ParentForm.Cursor = CType(IIf(showBusy, Cursors.WaitCursor, Cursors.Default), Windows.Forms.Cursor)
    End Sub

    Private Sub StartProcessing()
        SetFormStatus(FormStatus.Processing)
        backgroundArchiver.RunWorkerAsync(GetArchiveFileList())
    End Sub

    Private Sub ProcessingComplete(ByVal archiveIdList As List(Of String))

        Dim unassignedCount, totalCompleted As Integer
        DataScript.GetSummary(archiveIdList, totalCompleted, unassignedCount)
        Dim message = String.Format("Archiving completed successfully." + Environment.NewLine + "Total Confirms: {0}" + Environment.NewLine + "Unassigned Confirms: {1}", totalCompleted, unassignedCount)
        If unassignedCount > 0 Then
            message += Environment.NewLine + "Do you want to complete the unassigned confirms?"
            If MessageBox.Show(message, "Archiving Complete", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
                GoToFinishArchiving(archiveIdList)
            Else
                GoHome()
            End If
        Else
            MessageBox.Show(message, "Archiving Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
            GoHome()
        End If
    End Sub

    Private Sub backgroundArchiver_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles backgroundArchiver.ProgressChanged
        lblStatus.Text = e.UserState.ToString()
    End Sub

    Private Sub backgroundArchiver_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles backgroundArchiver.RunWorkerCompleted
        SetFormStatus(FormStatus.Idle)
        If e.Error Is Nothing Then
            ProcessingComplete(TryCast(e.Result, List(Of String)))
        Else
            MessageBox.Show(InterfaceHelper.GetFriendlyMessage(FormTypes.StartArchiving, e.Error), "Archiving Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub StartArchiving_PageEnter(ByVal sender As System.Object, ByVal e As Microsoft.Samples.Windows.Forms.Navigation.PageEventArgs) Handles MyBase.PageEnter
        SetFormStatus(FormStatus.Idle)
        Dim archiveFileList = Processor.GetArchiveFileSet()

        lnkDownloadConfirms.Visible = SystemSettings.Instance.IsAplEnabled

        chkArchiveFileList.Items.Clear()
        For Each file In archiveFileList
            chkArchiveFileList.Items.Add(file, True)
        Next

    End Sub

    Private Function GetArchiveFileList() As String()
        Dim length As Integer = chkArchiveFileList.CheckedItems.Count - 1
        Dim archiveFileList(length) As String

        For index = 0 To length
            archiveFileList(index) = CStr(chkArchiveFileList.CheckedItems(index))
        Next

        Return archiveFileList
    End Function


    Private Sub chkArchiveFileList_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkArchiveFileList.SelectedValueChanged
        btnStartArchiving.Enabled = (chkArchiveFileList.CheckedItems.Count > 0)
    End Sub

    Private Sub lnkDownloadConfirms_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkDownloadConfirms.LinkClicked
        GoToPage(FormTypes.DownloadConfirms)
    End Sub

    Public Overrides Function CanLeave() As Boolean
        Return Not backgroundArchiver.IsBusy
    End Function
End Class